<template>
  <el-dialog
    class="base-dialog"
    :custom-class="'panna-dialog_small'"
    :title="title"
    :visible.sync="dialogVisible"
    :append-to-body="true"
    align="center"
    :before-close="handleClose"
  >
    <el-table
      ref="table"
      :data="tableData"
      fit
      highlight-current-row
      height="200px"
      style="width: 100%"
    >
      <el-table-column label="项目名称">
        <template slot-scope="scope">{{
          scope.row.detectionItemName
        }}</template>
      </el-table-column>
      <el-table-column label="协议价1(元)">
        <template slot-scope="scope">
          <el-radio
            v-model="scope.row.agreementPriceState"
            :disabled="readOnlyDetectionItemPrice"
            :label="1"
            >{{ stringPriceFormat(scope.row.firstAgreementPrice) }}</el-radio
          >
        </template>
      </el-table-column>
      <el-table-column label="协议价2(元)">
        <template slot-scope="scope">
          <el-radio
            v-model="scope.row.agreementPriceState"
            :disabled="readOnlyDetectionItemPrice"
            :label="2"
            >{{ stringPriceFormat(scope.row.secondAgreementPrice) }}</el-radio
          >
        </template>
      </el-table-column>
      <el-table-column label="协议价3(元)">
        <template slot-scope="scope">
          <el-radio
            v-model="scope.row.agreementPriceState"
            :disabled="readOnlyDetectionItemPrice"
            :label="3"
            >{{ stringPriceFormat(scope.row.thirdAgreementPrice) }}</el-radio
          >
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="base-dialog-footer">
      <el-button
        v-if="!readOnlyDetectionItemPrice"
        v-loading="submitLoad"
        type="primary"
        @click="submitForm"
        >确 定</el-button
      >
      <el-button @click="handleClose">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { stringPriceFormat } from "../../../utils/StringFormat";
import { getDetectionItem } from "../../../api/EntrustManage/EntrustOrderPrice";
export default {
  name: "ChoiceAgreementPriceDialog",
  components: {},
  props: {
    readOnlyDetectionItemPrice: {
      type: Boolean,
      default: false,
    },
    detectionItemData: {
      type: Array,
      default: () => [],
    },
    agreementPriceDialogTableData: {
      type: Array,
      default: () => [],
    },
    sampleRegistrationId: {
      type: String,
      default: "",
    },
    agreementPriceType: {
      type: String,
      default: "",
    },

    entrustOrderId: {
      type: String,
      default: "",
    },
    value: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      dialogVisible: false,
      title: "选择协议价",
      submitLoad: false,
      formData: {},
      tableData: [],
    };
  },
  watch: {
    value: function() {
      this.dialogVisible = this.value;
      if (this.value) {
        this.init();
      }
    },
  },
  methods: {
    stringPriceFormat,
    init() {
      if (
        this.agreementPriceDialogTableData === null ||
        this.agreementPriceDialogTableData.length === 0
      ) {
        var id = 0;
        if (
          this.entrustOrderId !== 0 &&
          this.entrustOrderId !== "0" &&
          this.entrustOrderId !== ""
        ) {
          id = this.entrustOrderId;
        } else if (
          this.sampleRegistrationId !== 0 &&
          this.sampleRegistrationId !== "0" &&
          this.sampleRegistrationId !== ""
        ) {
          id = this.sampleRegistrationId;
        }
        // 查询委托单检测项目
        getDetectionItem({ id: id }).then((res) => {
          this.tableData = res.data;
          if (this.agreementPriceType !== "") {
            for (let i = 0; i < this.tableData.length; i++) {
              if (this.agreementPriceType === "firstAgreementPrice") {
                this.tableData[i].agreementPriceState = 1;
              } else if (this.agreementPriceType === "secondAgreementPrice") {
                this.tableData[i].agreementPriceState = 2;
              } else if (this.agreementPriceType === "thirdAgreementPrice") {
                this.tableData[i].agreementPriceState = 3;
              }
            }
          } else {
            if (this.detectionItemData != null) {
              for (let i = 0; i < this.tableData.length; i++) {
                for (let o = 0; o < this.detectionItemData.length; o++) {
                  if (
                    this.tableData[i].id ===
                    this.detectionItemData[o].detectionItemId
                  ) {
                    this.tableData[i].agreementPriceState =
                      this.detectionItemData[o].detectionItemPriceCode;
                  }
                }
              }
            }
          }
        });
      } else {
        this.tableData = this.agreementPriceDialogTableData;
      }
    },
    // 确定
    submitForm() {
      this.$emit("closeDialog", this.tableData);
      this.handleClose();
    },
    // 关闭弹框
    handleClose() {
      this.$emit("input", false);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
